package com.baselibrary.test.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.baselibrary.libs.Utils.OtherUtils;
import com.baselibrary.libs.Utils.log.LogUtils;
import com.baselibrary.libs.View.bar.initAction_Bar;
import com.baselibrary.libs.base.baseActivity.BaseActivity;
import com.baselibrary.test.R;
import com.baselibrary.test.app.AppContext;
import com.bumptech.glide.Glide;

import java.io.File;
import java.util.ArrayList;

/**13
 * 照片 回显
 */
public class Main13Activity extends BaseActivity implements View.OnClickListener {
    public final static int RESULT_CAPTURE = 3501;//从照相机
    public final static int RESULT_IMAGE = 3502;//从相册
    private Button bt;
    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private ImageView iv4;
    private ImageView iv5;
    private ImageView iv6;
    private initAction_Bar action_bar;
    private File mTmpFile;
    private ArrayList<ImageView> mImageViews = new
            ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getlayouXML() {
        return R.layout.activity_main13;
    }

    @Override
    public void initP() {

    }

    @Override
    public void initView() {

        bt = (Button) findViewById(R.id.bt);
        bt.setOnClickListener(this);
        iv1 = (ImageView) findViewById(R.id.iv1);
        iv2 = (ImageView) findViewById(R.id.iv2);
        iv3 = (ImageView) findViewById(R.id.iv3);
        iv4 = (ImageView) findViewById(R.id.iv4);
        iv5 = (ImageView) findViewById(R.id.iv5);
        iv6 = (ImageView) findViewById(R.id.iv6);
        action_bar = (initAction_Bar) findViewById(R.id.action_bar);

        action_bar.set_Title("照相回显");

        mImageViews.add(iv1);
        mImageViews.add(iv2);
        mImageViews.add(iv3);
        mImageViews.add(iv4);
        mImageViews.add(iv5);
        mImageViews.add(iv6);


    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt:
                camera();
                break;
        }
    }
    /**
     * 点了照相
     */
    public void camera(){
//        Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//            //创建照片名
//            cameraPath = String.format("%1$s%2$s.jpg", formatpath, getNameByData());
//            intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(cameraPath)));//打开相机,并传过去相片名
//            startActivityForResult(intent1, RESULT_CAPTURE);
        // 跳转到系统照相机
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if(cameraIntent.resolveActivity(getPackageManager()) != null){
            // 设置系统相机拍照后的输出路径
            // 创建临时文件
            mTmpFile = OtherUtils.createFile(getApplicationContext());
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTmpFile));
            startActivityForResult(cameraIntent, RESULT_CAPTURE);
        }else{
//            Toast.makeText(getApplicationContext(),
//                    R.string.msg_no_camera, Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * 点了相册
     */
    public void image(){
        Intent intent2 = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent2.setType("image/*");
        intent2.putExtra("return-data", true);
        startActivityForResult(intent2, RESULT_IMAGE);
    }

    private Bitmap bm;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_CAPTURE && resultCode == RESULT_OK) {//从照相机成功返回
//            Uri parse = Uri.parse(cameraPath);
//            String s = "file://" + cameraPath;
            // Uri parse = Uri.parse(mTmpFile);
            final String s = "file://" + mTmpFile;
            // Log.e("parse", parse + "");
            LogUtils.i("返回来的照相机图片路径"+s);
//            Glide
//                    .with(AppContext.getApplication())
//                    .load(s)
//                    .into(iv1);
            for (int i = 0; i < mImageViews.size(); i++) {
                ImageView imageView = mImageViews.get(i);
                //没有图片就设置
                if (null ==imageView.getTag()) {
                    Glide
                            .with(AppContext.getApplication())
                            .load(mTmpFile)
                            .into(imageView);

                    imageView.setTag(true);
                    break;
                }

            }
            // String url = "http://192.168.4.102:8080/my/upload";
            // "http://192.168.0.102/qichen/upload_img.php?app=xingxingdai&loan_area=4&loan_id=269";
//            String token = Share.getToken(this);
//            String loan_area = Share.getString(this, "loan_area");
//            String xingxing = "xingxingdai";
//
//            String url = Constants.up_image_url+"?"+"app="+xingxing+"&loan_area="+loan_area+"&loan_id="+token;
//            Uri parse = Uri.parse(s);
//            LogUtils.i("上传图片的过程"+url);
//            //上传图片的过程
//            InputStream stream = ImageUtil.compressImage(BitmapUtils.getResizeBitmap(this, parse,350,true));
//            // String fileName = s.substring(s.lastIndexOf("/") + 1);
//            String fileName = "xingxingdai_loan_id_"+"card_photo"+mClassTag+".jpg";
//            HttpMultipartPost post = new HttpMultipartPost(this, url,stream,fileName);
//            post.setCallBack(new HttpMultipartPost.CallBack() {
//                @Override
//                public void update(Integer i) {
//                    LogUtils.i("上传图片返回来的百分比"+i);
//                }
//            });
//            post.setCallBackMsg(new HttpMultipartPost.CallBackMsg() {
//                @Override
//                public void msg(JSONObject msg) {
//                    LogUtils.i("上传图片返回来的数据"+msg);
//                    //UtilsToast.showToast(ChoiceActivity.this, msg.getString("msg"));
//                    UtilsBroadcastReceiver.sendBroadcastReceiver(AppContext.getApplication(),"getDataSuccess",mClassTag+"",s);
//                }
//            });
//            post.execute();

          //  finish();
            //  myuploadFile(s);

    //    } else if (requestCode == RESULT_IMAGE && resultCode == RESULT_OK) {//从相册成功返回
//            Uri originalUri = data.getData();//图片路径
//            ContentResolver resolver = getContentResolver();
//            Log.e("originalUri", originalUri + "");
//            if (originalUri != null) {
//                //  myuploadFile(originalUri.getPath());
//                // LogUtils.i("返回来的相册路径"+originalUri.getPath());
//                try {
//                    bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);        //显得到bitmap图片
//                    String[] proj = {MediaStore.Images.Media.DATA};
//                    //好像是android多媒体数据库的封装接口，具体的看Android文档
//                    Cursor cursor = managedQuery(originalUri, proj, null, null, null);
//                    //按我个人理解 这个是获得用户选择的图片的索引值
//                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//                    //将光标移至开头 ，这个很重要，不小心很容易引起越界
//                    cursor.moveToFirst();
//                    //最后根据索引值获取图片路径
//                    final String path = cursor.getString(column_index);
//                    //myuploadFile(path);
//                    LogUtils.i("返回来的相册路径"+path);
//
//                    //String url = "http://192.168.4.102:8080/my/upload";
//                    String token = Share.getToken(this);
//                    String loan_area = Share.getString(this, "loan_area");
//                    String xingxing = "xingxingdai";
//
//                    String url = Constants.up_image_url+"?"+"app="+xingxing+"&loan_area="+loan_area+"&loan_id="+token;
//                    //String url = Constants.up_image_url;
//                    Uri parse = Uri.parse(path);
//                    LogUtils.i("上传图片的过程");
//                    //上传图片的过程
//                    InputStream stream = ImageUtil.compressImage(BitmapUtils.getResizeBitmap(this, parse,350,true));
//                    //String fileName = path.substring(path.lastIndexOf("/") + 1);
//                    String fileName = "xingxingdai_loan_id_"+"card_photo"+mClassTag+".jpg";
//                    HttpMultipartPost post = new HttpMultipartPost(this, url,stream,fileName);
//                    post.setCallBack(new HttpMultipartPost.CallBack() {
//                        @Override
//                        public void update(Integer i) {
//                            LogUtils.i("上传图片返回来的百分比"+i);
//                        }
//                    });
//                    post.setCallBackMsg(new HttpMultipartPost.CallBackMsg() {
//                        @Override
//                        public void msg(JSONObject msg) {
//                            LogUtils.i("上传图片返回来的数据"+msg);
//                            if (1==msg.getIntValue("result")){
//                                UtilsToast.showToast(AppContext.getApplication(), msg.getString("message"));
//                                UtilsBroadcastReceiver.sendBroadcastReceiver(AppContext.getApplication(),"getDataSuccess",mClassTag+"",path);
//                            }else{
//                                UtilsToast.showToast(AppContext.getApplication(), msg.getString("message"));
//                            }
//
//                        }
//                    });
//                    post.execute();
//                    //发送_图片路径_广播
//
//
//
//                    finish();
//                }catch (IOException e) {
//
//                    Log.e(TAG,e.toString());
//
      //          }

     //       }
        }
    }
}
